package com.admin.interfaces.web;

import java.io.IOException;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.admin.application.AdvertService;
import com.admin.application.BannerService;
import com.admin.application.OrderService;
import com.admin.application.PlayService;
import com.admin.conts.SessionKeys;
import com.admin.domain.modle.AjaxResponse;
import com.admin.domain.modle.Member;
import com.admin.domain.modle.Order;
import com.admin.domain.modle.Play;
import com.admin.infrastructure.RemoteDataUtil;
import com.admin.interfaces.facade.dto.OrderQueryDTO;
import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * description h5展示 User: WeiDa Date: 2017-4-11 Time: 16:41
 */
@Controller
@RequestMapping("/h5")
public class H5Controller extends BaseController {
	@Autowired
	protected PlayService playService;
	@Autowired
	protected BannerService bannerService;
	@Autowired
	protected AdvertService advertService;
	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(Model model) {
		model.addAttribute("bannerList", bannerService.list(null));
		model.addAttribute("advertList", advertService.list(null));
		if(getParameter("id")!=null)
		model.addAttribute("id", getParameter("id"));
		return "wap/index";
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("plays", playService.list());
		return "wap/list";
	}

	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(Model model, @PathVariable("id") int id) {

		Play play = playService.get(id);

		model.addAttribute("play",play );

		return "wap/detail";
	}

	@RequestMapping(value = "/seat/{id}/{type}", method = RequestMethod.GET)
	public String seat(Model model, @PathVariable("id") Integer playId,
			@PathVariable("type") Integer type,@CookieValue("userId") String userId) {
		Play p = playService.get(playId);
		model.addAttribute("play", p);
		model.addAttribute("userId",userId );
		String result = "wap/seat1";
		if (type == 2) {
			result = "wap/seat2";
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/seats/{playId}", method = RequestMethod.GET)
	public AjaxResponse seats(@PathVariable("playId") Integer playId,
            @RequestParam("saleDate") String saleDate){
	    String playDate = saleDate + " 00:00:00";
	    String seats = RemoteDataUtil.postHaiyinSeatList(playDate,
                playId.toString());
	    return AjaxResponse.success(JSON.parse(seats));
	}

	/**
	 * description 我的 User: WeiDa Date: 2017-4-24 Time: 22:35
	 */
	@RequestMapping(value = "/member", method = RequestMethod.GET)
	public String member(Model model,
			@RequestParam(name = "status", defaultValue = "") String status) {
		Member member = (Member) getSession().getAttribute(
				SessionKeys.OAUTH_USER_KEY);
		if (member != null) {
			OrderQueryDTO q = new OrderQueryDTO();
			q.setOpenId(member.getOpenId());
			q.setStatus(status);
			List<Order> orders = orderService.list(q);
			model.addAttribute("orders", orders);
		}
		return "wap/member";
	}



}
